package com.service.database.config;

import com.service.database.action.UrlAssembler;
import lombok.Data;

/**
 * 数据库公共对象
 * @author xianggj
 * @Date 2021/6/4 18:33
 **/
@Data
public class DatabaseConfig {
    /**
     * 数据库类型
     */
    private DataBase type;
    /**
     * 数据库IP
     */
    private String host;
    /**
     * 数据库端口
     */
    private Integer port;
    /**
     * 数据库名称
     */
    private String dbName;
    /**
     * 连接数据库模式名
     */
    private String schema;
    /**
     * 数据库用户名
     */
    private String userName;
    /**
     * 数据库密码
     */
    private String userPwd;

    private String url;

    private String driver;

    /**
     * 表名
     */
    private String tableName;


    public static final class DatabaseConfigBuilder {
        private DataBase type;
        private String host;
        private Integer port;
        private String dbName;
        private String schema;
        private String userName;
        private String userPwd;
        private String url;
        private String driver;
        private String tableName;

        private DatabaseConfigBuilder() {
        }

        public static DatabaseConfigBuilder aDatabaseConfig() {
            return new DatabaseConfigBuilder();
        }

        public DatabaseConfigBuilder withType(DataBase type) {
            this.type = type;
            return this;
        }
        public DatabaseConfigBuilder withType(String type) {
            this.type = DataBase.toDataBase(type);
            return this;
        }

        public DatabaseConfigBuilder withIp(String host) {
            this.host = host;
            return this;
        }

        public DatabaseConfigBuilder withPort(Integer port) {
            this.port = port;
            return this;
        }

        public DatabaseConfigBuilder withDbName(String dbName) {
            this.dbName = dbName;
            return this;
        }

        public DatabaseConfigBuilder withSchema(String schema) {
            this.schema = schema;
            return this;
        }

        public DatabaseConfigBuilder withUserName(String userName) {
            this.userName = userName;
            return this;
        }

        public DatabaseConfigBuilder withUserPwd(String userPwd) {
            this.userPwd = userPwd;
            return this;
        }

        public DatabaseConfigBuilder withUrl(String url) {
            this.url = url;
            return this;
        }

        public DatabaseConfigBuilder withDriver(String driver) {
            this.driver = driver;
            return this;
        }

        public DatabaseConfigBuilder withTableName(String tableName) {
            this.tableName = tableName;
            return this;
        }

        public DatabaseConfig build() {
            DatabaseConfig databaseConfig = new DatabaseConfig();
            databaseConfig.userName = this.userName;
            databaseConfig.schema = this.schema;
            databaseConfig.url = this.url;
            databaseConfig.dbName = this.dbName;
            databaseConfig.type = this.type;
            databaseConfig.userPwd = this.userPwd;
            databaseConfig.driver = this.driver;
            databaseConfig.port = this.port;
            databaseConfig.tableName = this.tableName;
            databaseConfig.host = this.host;
            return databaseConfig;
        }
        /**
         * 重新设置url
         * 有些地方需要加上dbName才能进行正确的查询
         */
        public DatabaseConfig resetUrlAndBuild(){
            DatabaseConfig databaseConfig = new DatabaseConfig();
            databaseConfig.userName = this.userName;
            databaseConfig.schema = this.schema;
            databaseConfig.dbName = this.dbName;
            databaseConfig.type = this.type;
            databaseConfig.userPwd = this.userPwd;
            databaseConfig.driver = this.driver;
            databaseConfig.port = this.port;
            databaseConfig.tableName = this.tableName;
            databaseConfig.host = this.host;
            databaseConfig.url = UrlAssembler.UrlAssemblerBuilder.anUrlAssembler()
                    .withDataBase(this.type)
                    .withHost(this.host)
                    .withPort(this.port)
                    .withDbName(this.dbName)
                    .withSchema(this.schema)
                    .build().createUrl();
            return databaseConfig;
        }
    }
}
